package com.csesteel.crm.control;

import com.csesteel.crm.domain.CrmMenu;
import com.csesteel.crm.domain.CrmPriviledge;
import com.csesteel.crm.domain.CrmRole;
import com.csesteel.crm.domain.User;
import com.csesteel.crm.service.CrmMenuService;
import com.csesteel.crm.service.CrmPriviledgeService;
import com.csesteel.crm.service.CrmRoleService;
import com.csesteel.crm.service.UserService;
import com.csesteel.crm.util.MD5Util;
import com.csesteel.crm.util.WebUtil;
import com.csesteel.crm.vo.ResponseInfo;
import com.csesteel.crm.vo.ResponseMenuInfo;
import com.csesteel.crm.vo.UserSessionInfor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/crm")
public class PriviledgeController extends BaseController{
    private Log logger = LogFactory.getLog(PriviledgeController.class);
    @Autowired
    private CrmPriviledgeService crmPriviledgeService;

    @Autowired
    private CrmMenuService crmMenuService;



    /**
     * 根据分页获取所有的权限
     * @param request
     * @param response
     */
    @RequestMapping("/getPriviledgeByPage")
    public void getPriviledgeByPage(HttpServletRequest request,HttpServletResponse response) {
        int pageNumber =Integer.parseInt(request.getParameter("pageNumber"));
        int pageSize =Integer.parseInt(request.getParameter("pageSize"));
        String priviledgeCode = request.getParameter("priviledgeCode");
        String total = null;
        List<CrmPriviledge> crmPriviledgeList = crmPriviledgeService.findAllCrmPriviledgeByPage(pageNumber, pageSize, priviledgeCode);
        total =String.valueOf(crmPriviledgeService.findCount(priviledgeCode));
        ResponseMenuInfo responseInfo = new ResponseMenuInfo();
        responseInfo.setList(crmPriviledgeList);
        responseInfo.setTotalNumber(total);
        WebUtil.responseToJson(response,responseInfo);
    }

    /**
     * 获取菜单对应的资源
     * @param request
     * @param response
     */
    @RequestMapping("/getPriviledgeByMenu")
    public void getPriviledgeByMenu(HttpServletRequest request,HttpServletResponse response) {
        String priviledgeId = request.getParameter("priviledgeId");
        if(priviledgeId != null && !"".equals(priviledgeId))
        {
            int ipriviledgeId =Integer.parseInt(request.getParameter("priviledgeId"));
            CrmPriviledge crmPriviledge = crmPriviledgeService.getCrmPriviledge(ipriviledgeId);
            List<CrmMenu> crmMenuList = crmMenuService.findAllCrmMenu();
            ResponseMenuInfo responseInfo = new ResponseMenuInfo();
            responseInfo.setList(crmMenuList);
            responseInfo.setObject(crmPriviledge);
            WebUtil.responseToJson(response,responseInfo);
        }

    }

    /**
     * 保存菜单权限对应的资源
     * @param request
     * @param response
     */
    @RequestMapping("/savePriviledgeByMenu")
    public void savePriviledgeByMenu(HttpServletRequest request,HttpServletResponse response) {
        RequestContext requestContext = new RequestContext(request);
        String priviledgeId = request.getParameter("priviledgeId");
        String[] menuIds = request.getParameterValues("menuId[]");
        if(priviledgeId != null && !"".equals(priviledgeId))
        {
            int ipriviledgeId =Integer.parseInt(request.getParameter("priviledgeId"));
            crmPriviledgeService.deletePriviledgeByMenu(ipriviledgeId); //删除关联的菜单
            if(menuIds !=null && menuIds.length>0)
            {
                for(String menuId :menuIds)
                {
                    int imenuId = Integer.parseInt(menuId);
                    crmPriviledgeService.savePriviledgeByMenu(ipriviledgeId,imenuId);
                }
                ResponseMenuInfo responseInfo = new ResponseMenuInfo();
                responseInfo.setResult("0");
                responseInfo.setMessage(requestContext.getMessage("privilege.menu.success"));
                WebUtil.responseToJson(response,responseInfo);
            }

        }

    }

    /**
     * 修改权限
     * @param request
     * @param response
     */
    @RequestMapping("/updatePriviledgeById")
    public void updatePriviledgeById(@ModelAttribute("crmPriviledge") CrmPriviledge crmPriviledge,HttpServletRequest request,HttpServletResponse response) {

        if (crmPriviledge != null)
        {
            crmPriviledgeService.updateCrmPriviledge(crmPriviledge);
        }
    }

    /**
     * 保存权限
     * @param request
     * @param response
     */
    @RequestMapping("/savePriviledgeById")
    public void savePriviledgeById(@ModelAttribute("crmPriviledge") CrmPriviledge crmPriviledge,HttpServletRequest request,HttpServletResponse response) {

        if (crmPriviledge != null)
        {
            crmPriviledgeService.saveCrmPriviledge(crmPriviledge);
        }
    }

    /**
     * 删除权限
     * @param request
     * @param response
     */
    @RequestMapping("/deletePriviledgeById")
    public void deletePriviledgeById(HttpServletRequest request,HttpServletResponse response) {

        int ipriviledgeId =Integer.parseInt(request.getParameter("priviledgeId"));
        crmPriviledgeService.deleteCrmPriviledge(ipriviledgeId);
    }
}